package com.e1xp.deport_management_system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.e1xp.deport_management_system.entity.Order;
import com.e1xp.deport_management_system.utils.PickRack;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 获取最近count条订单
     *
     * @param count 获取条目数
     * @return List order列表
     */
    @Select("SELECT * FROM `order` WHERE instance_id=#{instanceId} and stats=0 ORDER BY order_time DESC LIMIT #{count}")
    List<Order> getRecent(@Param("count") int count, @Param("instanceId") long instanceId);

    @Select("SELECT count(*) from `order` where stats!=3 and instance_id=#{instanceId}")
    long getUnFinishCount(long instanceId);

    @Select("SELECT order_product.id as id," +
            "p.id as prodcutId," +
            "p.name as name," +
            "order_product.count as count," +
            "order_product.stats as status," +
            "p.price as price," +
            "order_product.price as sumPrice " +
            "FROM order_product " +
            "left join product p on p.id = order_product.product_id " +
            "where order_product.instance_id=#{instanceId} " +
            "and order_id=#{orderId}")
    List<Map<String, String>> getProductList(@Param("instanceId") long instanceId, @Param("orderId") long orderId);

    @Select("SELECT op.product_id as productId," +
            "rack_id as id," +
            "concat(r.id,' ',r.name,' 具有:',pr.count,'个') as text " +
            "from order_product op " +
            "left join product p on p.id = op.product_id " +
            "left join product_rack pr on p.id = pr.product_id " +
            "left join rack r on r.id = pr.rack_id " +
            "where op.order_id=#{orderId}")
    List<PickRack> getPickRack(long orderId);

    @Select("SELECT op.product_id as productId," +
            "p.name as name," +
            "(op.count-op.pick_count) as count " +
            "FROM order_product op " +
            "left join product p on p.id = op.product_id " +
            "left join `order` o on o.id = op.order_id " +
            "where op.order_id=#{orderId} and op.stats=1")
    List<Map<String, String>> getToPickProduct(long orderId);
}
